from flask import Flask, render_template, request, jsonify 
from data.data import database
import json
import argparse

app = Flask(__name__) 

Foreign_data_source = database("Foreign")
Domestic_data_source = database("Domestic")
Dataset_data_source = database("Dataset")
Isomerism_data_source = database("Isomerism")
Startups_data_source = database("Startups")
data_source = {}
data_source.update(Foreign_data_source)
data_source.update(Domestic_data_source)
data_source.update(Dataset_data_source)
data_source.update(Isomerism_data_source)
data_source.update(Startups_data_source)
# with open('data.json','w')as f:
#     json.dump(data_source, f, indent=4, ensure_ascii=False)

@app.route('/') 
def index(): 
    return render_template('index.html') 
 
@app.route('/square/', methods=['POST']) 
def square(): 
    num = float(request.form.get('number', 0)) 
    square = num ** 2 
    data = {'square': square} 
    data = jsonify(data) 
    return data 

@app.route('/call-python-function') 
def call_python_function(): 
    # labels = request.json()
    # print(labels)
    # Your Python function code here 
    return jsonify({'data': '未知标签'})

@app.route('/get_data', methods=['POST'])
def get_data():
    # 获取前端发送的标签列表
    label = request.json.get('labels','')
    # 根据标签从数据源中获取对应的内容
    # result = {label:str( data_source.get(label, '未知内容')) for label in labels}
    result = data_source.get(label, '未知内容')
    # 返回结果
    return jsonify(result)


@app.route('/api/foreign', methods=['GET'])
def get_dropdown_foreign():
    Foreign_backendData=[{"label": key, "value": key} for key in Foreign_data_source] 
    return jsonify(Foreign_backendData)
 

@app.route('/api/domestic', methods=['GET'])
def get_dropdown_domestic(): 
    Domestic_backendData=[{"label": key, "value": key} for key in Domestic_data_source]
    return jsonify(Domestic_backendData)
 

@app.route('/api/startups', methods=['GET'])
def get_dropdown_startups(): 
    Startups_backendData=[{"label": key, "value": key} for key in Startups_data_source]
    return jsonify(Startups_backendData)
 

@app.route('/api/Isomerism', methods=['GET'])
def get_dropdown_Isomerism(): 
    Isomerism_backendData=[{"label": key, "value": key} for key in Isomerism_data_source]
    # Isomerism_backendData=[{}]
    return jsonify(Isomerism_backendData)
 
@app.route('/api/datasets', methods=['GET'])
def get_dropdown_datasets(): 
    Datasets_backendData=[{"label": key, "value": key} for key in Dataset_data_source]
    return jsonify(Datasets_backendData)

if __name__ == '__main__': 
    parser=argparse.ArgumentParser(description="host & port")
    parser.add_argument('--host', type=str, default='172.31.226.156', help='ip')
    parser.add_argument('--port', type=int, default=5000, help='port')
    args=parser.parse_args()
    app.run(host=args.host, port=args.port, debug=True) 
